<?php

namespace app\api\controller;

use think\Db;

class Villages extends ApiController
{
    public function Vill()
    {
        $this->db = Db::table('sms_vg_villages');
        return apiSuccess(parent::datalist());
    }
    public function ActiveType()
    {
        $this->db = Db::table('sms_activetype');
        return apiSuccess(parent::datalist());
    }
    public function Minsu($vill_id)
    {
        $this->db = Db::table('sms_vg_minsu')
            ->alias('v')
            ->field('v.*')
            ->where('v.vill_id', $vill_id)
            ->find();

        if ($this->db) {
            return apiSuccess($this->db);
        } else {
            return apiError('Village not found');
        }
    }
    public function VillWithActivities($id)
    {

        $this->db =
            Db::table('sms_vg_activity')
            ->alias('a')
            ->join('sms_vg_villages v', 'v.id = a.vill_id', 'LEFT')
            ->join('sms_activetype t', 't.id = a.type_id', 'LEFT')
            ->field('a.*, t.*')
            ->where('v.id', $id)
            ->select();
        return apiSuccess($this->db);
    }
    public function VillWithSpeacials($id)
    {

        $this->db = Db::table('sms_specialpro') //改成sms_vg_product
            ->alias('a')
            ->join('sms_vg_villages v', 'v.id = a.vill_id', 'LEFT')
            ->field('a.*')
            ->where('v.id', $id)
            ->select();

        return apiSuccess($this->db);
    }
    public function VillDetail($id)
    {

        $this->db = Db::table('sms_vg_villages')
            ->alias('v')
            ->field('v.*')
            ->where('v.id', $id)
            ->find();

        if ($this->db) {
            return apiSuccess($this->db);
        } else {
            return apiError('Village not found');
        }
    }
    public function VillWithSpeohis($id)
    {
        $this->db = Db::table('sms_vg_peohis')
            ->alias('a')
            ->join('sms_vg_villages v', 'v.id = a.vill_id', 'LEFT')
            ->field('a.*')
            ->where('v.id', $id)
            ->select();
        return apiSuccess($this->db);
    }
    public function Villminsu($id)
    {
        $this->db = Db::table('sms_vg_minsu')
            ->alias('v')
            ->field('v.*')
            ->where('v.id', $id)
            ->find();

        if ($this->db) {
            return apiSuccess($this->db);
        } else {
            return apiError('Village not found');
        }
    }
}
